package edu.tjut.tjutcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.tjut.tjutcommunity.entity.Postlogs;
import edu.tjut.tjutcommunity.entity.User;
import edu.tjut.tjutcommunity.mapper.PostlogsMapper;
import edu.tjut.tjutcommunity.service.PostlogsService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-04-15
 */
@Service
public class PostlogsServiceImpl extends ServiceImpl<PostlogsMapper, Postlogs> implements PostlogsService {
    final
    PostlogsMapper postlogsMapper;

    public PostlogsServiceImpl(PostlogsMapper postlogsMapper) {
        this.postlogsMapper = postlogsMapper;
    }

    @Override
    public boolean insert(User user, Integer pid, Integer degree) {
        Postlogs postlogs = new Postlogs();
        postlogs.setUid(user.getUid());
        postlogs.setPid(pid);
        postlogs.setPreferDegree(degree);
        return postlogsMapper.insert(postlogs) == 1;
    }

    @Override
    public List<Postlogs> getPostlogsHistory(String specificDayFormat) {
        QueryWrapper<Postlogs> qw = new QueryWrapper<>();
        qw.gt("create_time", specificDayFormat);
        return postlogsMapper.selectList(qw);
    }

    @Override
    public boolean delete(User user, Integer pid, Integer degree) {
        QueryWrapper<Postlogs> qw = new QueryWrapper<>();
        qw.eq("uid", user.getUid());
        qw.eq("pid", pid);
        qw.eq("degree", degree);
        return postlogsMapper.delete(qw) == 1;
    }

    @Override
    public Set<Integer> getHasViewId(User user) {
        Integer uid = user.getUid();
        if (uid==null) return Collections.EMPTY_SET;
        QueryWrapper<Postlogs> qw = new QueryWrapper<>();
        qw.eq("uid",uid);
        List<Postlogs> postlogs = postlogsMapper.selectList(qw);
        return postlogs.stream().map(Postlogs::getPid).collect(Collectors.toSet());
    }
}
